<?php

if( !defined( 'MOODLE_INTERNAL' ) )
{
    die( 'Direct access to this script is forbidden.' );    ///  It must be included from a Moodle page
}

require_once($CFG->dirroot .'/config.php');
require_once($CFG->dirroot .'/lib/moodlelib.php');
require_once($CFG->dirroot .'/blocks/live_services_sso/lib/GetSLT.php');


class block_live_services_sso extends block_base 
{
	function init() 
	{
		$this->title = get_string('blockname', 'block_live_services_sso');
		$this->version = 2004111209;
	}

	function get_content() 
	{
		global $USER;
		global $PAGE;
		global $CFG;

		$isLoggedInUrl = "https://login.live.com/LOGIN.srf?wa=wsignin1.0&rpsnv=45&ct=1214844864&rver=4.5.2130.0&wp=NFS_2HR_0_COMPACT&wreply=http:%2F%2Faccount.live.com%2Fdefault.aspx&id=60969&AJ=1";

		// Local library
		$content_html = '';
		$content_html .= "<script type=\"text/javascript\" src=\"http://elr3.familyandfriends.adambradleyconsulting.com/moodle/blocks/live_services_sso/js/library.js\"></script>";

		// Logger and loggedInImage
		$content_html .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"http://elr3.familyandfriends.adambradleyconsulting.com.com/moodle/lib/yui/assets/skins/sam/logger.css\">";
		$content_html .= "<script type=\"text/javascript\" src=\"http://elr3.familyandfriends.adambradleyconsulting.com/moodle/lib/yui/yahoo-dom-event/yahoo-dom-event.js\"</script>";
		$content_html .= "<script type=\"text/javascript\" src=\"http://elr3.familyandfriends.adambradleyconsulting.com/moodle/lib/yui/logger/logger-min.js\"</script>";
		$content_html .= "<script type=\"text/javascript\" src=\"http://elr3.familyandfriends.adambradleyconsulting.com/moodle/lib/yui/dragdrop/dragdrop-min.js\"></script>"; 

		// Make the SLT call
	        $getSLT = new GetSLT();
	        $getSLT->siteId = "256296";
	        $getSLT->sslcert = "/srv/www/htdocs/private-cer.pem";
	        $getSLT->sslkey = "/srv/www/htdocs/private.pem";
	        $getSLT->cainfo = "/srv/www/htdocs/all.pem";
	        $slt = $getSLT->Request($USER->email,"30");
		$redirectUrl = $getSLT->exchangeLabsUrl . "&slt=" . $slt;

		$content_html .= "<div id = \"ssoDiv\"></div>";
		$content_html .= "<script>
document.body.className += \" yui-skin-sam\";
YAHOO.log(\"Checking signed in state image\");
SSO.init(\"" . $redirectUrl . "\",\"" . $isLoggedInUrl . "\");
SSO.isLoggedIn();
YAHOO.lang.later(30000, SSO, \"isLoggedIn\", null, true);
</script>";

        	// store the value and return the content
	        $this->content = new stdClass;
        	$this->content->text = $content_html;
	        $this->content->footer = '';
		return $this->content;
	}

	function instance_allow_config() 
	{
		return true;
	}
	
	function html_attributes() 
	{
		return array(
		);
	}

	function hide_header() {
  		return false;
	}
}
?>
